package zhangzhifan;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
import java.awt.*;
//import org.eclipse.wb.swing.FocusTraversalOnArray;

/**
 * 
 * ���Һ��滻�Ի���
 * 
 * 
 * @author ��֪��
 */
public class SearchDlg extends JDialog{
	private JTextField srcField;
	private JTextField rplField;
	private JTextArea text;
	private String FindSource;
	private String ReplaceSource;
	private int Start = 0, End = 0;		//�������滻��ʼĩλ��
	private int FindCnt = 0; 			//���ҵ���Ŀ
	boolean Mode = false; 
	boolean caseSensitive = false;
	/**
	 * �ر�
	 */
	private void Close() {
		this.setVisible(false);
	}
	
	//���캯��
	public SearchDlg (JFrame owner, JTextArea textArea) {
		super(owner, "Find and Replace", false);
		setResizable(false);
		setSize(new Dimension(287, 300));
		//���Ҫ�����滻���ı���
		text = textArea;
		//����ʼĩλ��
		Start = 0; 
		End = text.getText().length();
		
		//���ù���
		JLabel searchLbl = new JLabel("Find");
			searchLbl.setBounds(55, 39, 26, 18);
		
		srcField = new JTextField();
			srcField.setBounds(138, 37, 114, 22);
			srcField.setColumns(10);
		
		JLabel replaceLbl = new JLabel("Replace");
			replaceLbl.setBounds(55, 72, 65, 18);
		
		rplField = new JTextField();
			rplField.setBounds(138, 70, 114, 22);
			rplField.setColumns(10);
			rplField.setEditable(false);
		
		final JCheckBox chckbxCaseSensitive = new JCheckBox("Case Sensitive");
		//�����Ƿ��滻ѡ�򣬲�������Ӧ����
		final JCheckBox ifReplace = new JCheckBox("");
		ifReplace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Mode = ifReplace.isSelected();
				rplField.setEditable(Mode);
			}
		});
		ifReplace.setBounds(20, 71, 21, 21);
		
		//�Ƿ���ִ�Сд
		chckbxCaseSensitive.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				caseSensitive = chckbxCaseSensitive.isSelected();
			}
		});
		chckbxCaseSensitive.setBounds(20, 107, 111, 26);
		
		final JLabel resultLabel = new JLabel("Result");
			resultLabel.setBounds(20, 140, 232, 18);
		
		JButton FindNextButton = new JButton("Find Next");
			FindNextButton.setBounds(20, 173, 111, 28);
		
		//��������һ��������Ӧ
			FindNextButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					getSrc();
					if (FindSource.length() == 0)
						return;
					FindCnt = 0;
					if (Start < 0) {
						return;
					}
					String content = text.getText();
					
					if(!caseSensitive) {
						content = content.toLowerCase();
						FindSource = FindSource.toLowerCase();
					}
					
					int StartForCount = 0, FindPosForCount = 0;
					while(FindPosForCount != -1) {
						FindPosForCount = content.indexOf(FindSource, StartForCount);
						StartForCount = FindPosForCount + 1;
						if(FindPosForCount != -1)
							FindCnt++;
					}
					int findPos = text.getText().indexOf(FindSource, Start);
					if (FindCnt == 0) {
						JOptionPane.showMessageDialog(null, "δ�ҵ�\"" + FindSource + '\"');
						return;
					}
					resultLabel.setText("���ҵ�" + FindCnt + "��");
					if (findPos == -1) {
						Start = 0;
						return;
					}
					else {
						text.setCaretPosition(findPos);
						text.select(findPos, findPos + FindSource.length());
						Start = findPos + 1;
					}
				}
			});
		
		JButton cancelButton = new JButton("Close");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Close();
				}
			});
			cancelButton.setBounds(20, 213, 232, 28);
		
		JButton ReplaceAllButton = new JButton("Replace All");
		
			//�滻ȫ�� ����Ӧ
			ReplaceAllButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getSrc();
					String content = text.getText();
					//ʹ��pattern���滻
					Pattern p = Pattern.compile(FindSource);
					Matcher m = p.matcher(content);
					StringBuffer sb = new StringBuffer();
					boolean result = m.find();
					while(result) {
						m.appendReplacement(sb, ReplaceSource);
						result = m.find();
					}
					m.appendTail(sb);
					text.setText(sb.toString());
				}
			});
			ReplaceAllButton.setBounds(137, 173, 115, 28);
			getContentPane().setLayout(null);
			getContentPane().add(resultLabel);
			getContentPane().add(FindNextButton);
			getContentPane().add(cancelButton);
			getContentPane().add(ReplaceAllButton);
			getContentPane().add(ifReplace);
			getContentPane().add(replaceLbl);
			getContentPane().add(searchLbl);
			getContentPane().add(srcField);
			getContentPane().add(rplField);
			getContentPane().add(chckbxCaseSensitive);
			//setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{srcField, ifReplace, rplField, chckbxCaseSensitive, FindNextButton, ReplaceAllButton, cancelButton}));
			
		}

	//��������textArea

	/**
	 * ��ò��Һ��滻���ַ�
	 */
	void getSrc() {
		FindSource = srcField.getText();
		ReplaceSource = rplField.getText();
	}
}


